/**
 * Copyright (c) 2012-2013, Michael Yang 杨福海 (www.yangfuhai.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.tsz.afinal.bitmap.display;

import net.tsz.afinal.bitmap.core.BitmapDisplayConfig;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

/**
 * 使加载完图片后，添加单击变暗效果
 * @author wly
 *
 */
public class SimpleDisplayer2 implements Displayer{
	
	public void loadCompletedisplay(ImageView imageView,Bitmap bitmap,BitmapDisplayConfig config){
		switch (config.getAnimationType()) {
		case BitmapDisplayConfig.AnimationType.fadeIn:
			fadeInDisplay(imageView,bitmap);
			break;
		case BitmapDisplayConfig.AnimationType.userDefined:
			animationDisplay(imageView,bitmap,config.getAnimation());
			break;
		default:
			break;
		}
	}
	
	
	public void loadFailDisplay(ImageView imageView,Bitmap bitmap){
		imageView.setImageBitmap(bitmap);
	}
	
	
	
	private void fadeInDisplay(final ImageView imageView,final Bitmap bitmap){
		AlphaAnimation animation = new AlphaAnimation(0.5f, 1.0f);
		animation.setDuration(500);
		animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());		
        imageView.setImageBitmap(bitmap);
        imageView.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				bondColorFilter(imageView, bitmap);
			}
		});
	}
	
	
	private void animationDisplay(final ImageView imageView,final Bitmap bitmap,Animation animation){
		animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());		
        imageView.setImageBitmap(bitmap);
        imageView.startAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				bondColorFilter(imageView, bitmap);
			}
		});
	}

	
	public void bondColorFilter(ImageView imageView,Bitmap originalBitmap) {
		float[] BT_SELECTED = new float[] { 0.75f, 0, 0, 0, 0, 0,
				0.75f, 0, 0, 0, 0, 0, 0.75f, 0, 0, 0, 0, 0,1, 0 };
		Bitmap one = originalBitmap;
	    Bitmap oneCopy = one.copy(Config.ARGB_8888, true);
	    
	    Canvas c = new Canvas(oneCopy);
	    Paint p = new Paint();
	    p.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
	    c.drawBitmap(one, 0, 0, p);

	    StateListDrawable states = new StateListDrawable();
	    states.addState(new int[] {android.R.attr.state_pressed}, new BitmapDrawable(oneCopy));
	    states.addState(new int[] { }, imageView.getDrawable());
	    imageView.setImageDrawable(states);
	}
	
	public void bondColorFilter(ImageView imageView,Drawable originalDrawable) {
		float[] BT_SELECTED = new float[] { 0.75f, 0, 0, 0, 0, 0,
				0.75f, 0, 0, 0, 0, 0, 0.75f, 0, 0, 0, 0, 0,1, 0 };
		BitmapDrawable bitmapDrawable = (BitmapDrawable)originalDrawable;
		Bitmap one = bitmapDrawable.getBitmap();
		if(one != null) {
		    Bitmap oneCopy = one.copy(Config.ARGB_8888, true);
		    
		    Canvas c = new Canvas(oneCopy);
		    Paint p = new Paint();
		    p.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
		    c.drawBitmap(one, 0, 0, p);

		    StateListDrawable states = new StateListDrawable();
		    states.addState(new int[] {android.R.attr.state_pressed}, new BitmapDrawable(oneCopy));
		    states.addState(new int[] { }, imageView.getDrawable());
		    imageView.setImageDrawable(states);
		}
	}
}
